<?php
/**
 * Created by PhpStorm.
 * User: yys
 * Date: 2016/12/2
 * Time: 9:14
 */

namespace App\Http\Controllers\Front;


use App\Services\ImgServices\ImgTool;
use App\Services\ModifyResourcePriceRecordsService\ModifyResourcePriceRecordsService;
use App\Services\SourcePicService\SourcePicFrontService;
use App\Services\SourcePicService\SourcePicTool;
use App\Services\Tool\GeneralTool;
use App\Services\UserService\UserTool;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Response;
class SourcePicFrontController
{
    protected $sourcePicFrontSer;
    protected $userTool;
    protected $modifyResourcePriceRecordsSer;
    protected $sourcePicTool;
    /**
     * SourcePicFrontController constructor.
     */
    public function __construct(SourcePicFrontService $sourcePicFrontSer,
                                ModifyResourcePriceRecordsService $modifyResourcePriceRecordsSer,
                                SourcePicTool $sourcePicTool,
                                UserTool $userTool)
    {
        $this->sourcePicFrontSer=$sourcePicFrontSer;
        $this->userTool=$userTool;
        $this->modifyResourcePriceRecordsSer=$modifyResourcePriceRecordsSer;
        $this->sourcePicTool=$sourcePicTool;
    }

    /***
     * 批量上传素材
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function bulkUploadSourcePic(Request $request)
    {
        return view('Front.sourcepic.bulkUploadSourcePic');
    }
    /***
     * dropzone 批量上传素材
     *
     * @param Request $request
     * @return mixed
     */
    public function postBulkUploadSourcePic(Request $request)
    {
        $rule=array(
            'price'=>'required|numeric|min:0',
            'file' => 'dimensions:min_width=1000,min_height=1000',
            'upload_type'=>'integer|min:0|max:1',
        );
        $validator=SourcePicTool::store($request,$rule);
        $image=$request->file('file');
        $price=floatval($request->input('price'));
        $data=$request->all();
        $platform=intval($request->input('upload_type'));
        if ($validator->fails())
        {
            if($validator->errors()->first('price')){
                $result['message']='定价格式错误';
            }else if($validator->errors()->first('file')){
                $result['message']='请上传尺寸大于1000x1000的图片';
            }
            $result['state']=-2;
            return response()->json($result);
        }
        $user_type=intval($request->session()->get('user_type'));
        $user_id=$request->session()->get('user_id');
        if(empty($user_type)||empty($user_id)){
            $result= GeneralTool::createResult('', 'M14001');
            return Response::json($result);
        }
        $userinfo=$request->session()->get('userinfo');
        $source_pic['file']=$image;
        $source_pic['user_id']=$user_id;
        $source_pic['platform']=2;
        $source_pic['price']=$price;
        $source_pic['audit_state']=0;
        $result=$this->sourcePicFrontSer->uploadSourcePicSimpleInfo($source_pic);
        if($result!==false){
            return Response::json(array('state'=>'M00000','message'=>'保存成功！','source_pic_id'=>$result));
        }else{
            return Response::json(array('state'=>'M00001','message'=>'保存失败！','source_pic_id'=>''));
        }

    }

    /***
     * 获取素材列表
     * @param Request $request
     * @return $this|\Illuminate\Http\RedirectResponse
     */
    public function getSourcePicList(Request $request)
    {
        $user_id=$request->session()->get('user_id');
        $is_shelves=$request->input('is_shelves');
        $audit_state=$request->input('audit_state');
        if($is_shelves==null){
            $is_shelves=-1;
        }else{
            $is_shelves=intval($is_shelves);
        }
        if($is_shelves!=0&&$is_shelves!=1){
         $is_shelves=-1;
        }
        if($audit_state==null){
            $audit_state=-1;
        }else{
            $audit_state=intval($audit_state);
        }
        if($audit_state!=0&&$audit_state!=1&&$audit_state!=2&&$audit_state!=3){
            $audit_state=-1;
        }
        if(empty($user_id)){
            $result= GeneralTool::createResult('', 'M14001');
            return Response::json($result);
        }
        $data['is_shelves']=$is_shelves;
        $data['user_id']=$user_id;
        $data['per_page']=10;
        $data['audit_state']=$audit_state;
        $source_pics=$this->sourcePicFrontSer->getSourcePicList($data);
        if($source_pics===false){
            return redirect()->back()->with('error_message', '暂无数据!');
        }
        foreach ($source_pics as $source_pic){
            $source_pic->original_image=ImgTool::getImgUrlByImgPath($source_pic->original_image);
            $source_pic->thumbnail=ImgTool::getImgUrlByImgPath($source_pic->thumbnail);
            $source_pic->small_image=ImgTool::getImgUrlByImgPath($source_pic->small_image);
            $user_id=$source_pic->user_id;
            $user_name='';
            if($user_id!=0){
                $userinfo=$this->userTool->userInfoByid($user_id);
                if(!empty($userinfo)){
                    $user_name=$userinfo->nick_name;
                }
            }

            $source_pic->user_name=$user_name;
        }
        return view('Front.sourcepic.sourcePicList')
            ->with('source_pics',$source_pics)
            ->with('is_shelves',$is_shelves)
            ->with('audit_state',$audit_state);
    }

    /***
     * 设计师修改素材单价
     * @param Request $request
     * @return mixed
     */
    /***
     * 更新后台上传素材价格
     * @param Request $request
     * @return mixed
     */
    public function updatePrice(Request $request){
        $rule=array(
            'price'=>'required|numeric|min:0',
            'id'=>'required|string|',
        );
        $validator=SourcePicTool::store($request,$rule);
        $id=trim($request->input('id'));
        $price=floatval($request->input('price'));
        if ($validator->fails())
        {
            if($validator->errors()->first('price')){
                $result['state']=-2;
                $result['message']='单价格式出错';
                return response()->json($result);
            }
            if($validator->errors()->first('id')){
                $result['state']=-3;
                $result['message']='素材编号格式出错';
                return response()->json($result);
            }
        }
        $source_pic=$this->sourcePicTool->getSourcePicBySourcePicId($id,1);
        if(empty($source_pic)){
            $result['state']=-3;
            $result['message']='数据不存在';
            return response()->json($result);
        }
        $user_id=$request->session()->get('user_id');
        if($source_pic->user_id!==$user_id){
            $result['state']=-4;
            $result['message']='无权修改';
            return response()->json($result);
        }
        $data['source_pic_id']=$id;
        $data['price']=$price;
        //价格要定时修改
        $input['modify_price']=$price;
        $input['price']=$source_pic->price;
        $input['resource_type']=1;
        $input['resource_id']=$id;
        $input['admin_id']=0;
        $input['admin_email']='';
        @$update_modify_price_result=$this->modifyResourcePriceRecordsSer->save_modify_resource_price_record($input);
        $result['state']=1;
        $result['message']='更新成功';
        return response()->json($result);
    }

    /***
     * 更新素材名称
     * @param Request $request
     * @return mixed
     */
    public function updateSourcePicName(Request $request){
        $rule=array(
            'name'=>'required|string|',
            'id'=>'required|string|',
        );
        $validator=SourcePicTool::store($request,$rule);
        $id=trim($request->input('id'));
        $name=trim($request->input('name'));
        if ($validator->fails())
        {
            if($validator->errors()->first('name')){
                $result['state']=-2;
                $result['message']='素材名称格式错误';
                return response()->json($result);
            }
            if($validator->errors()->first('id')){
                $result['state']=-3;
                $result['message']='素材编号格式出错';
                return response()->json($result);
            }
        }
        $source_pic=$this->sourcePicTool->getSourcePicBySourcePicId($id,1);
        if(empty($source_pic)){
            $result['state']=-3;
            $result['message']='数据不存在';
            return response()->json($result);
        }
        $user_id=$request->session()->get('user_id');
        if($source_pic->user_id!==$user_id){
            $result['state']=-4;
            $result['message']='无权修改';
            return response()->json($result);
        }
        $data['source_pic_id']=$id;
        $data['name']=$name;
        $res=$this->sourcePicFrontSer->updateSourcePicName($id,$name);
        if($res){
            //价格要定时修改
            $result['state']=1;
            $result['message']='更新成功';
        }else{
            //价格要定时修改
            $result['state']=-1;
            $result['message']='更新失败';
        }
        return response()->json($result);
    }

    /***
     *  更新素材
     * @param Request $request
     * @return $this|\Illuminate\Http\RedirectResponse
     */
    public function updateSourcePicPage(Request $request){
        $source_pic_id=trim($request->input('id'));
        if(empty($source_pic_id)){
            return redirect()->back()->with('error_message','系统繁忙');
        }
        $source_pic=SourcePicFrontService::getSourcePic($source_pic_id);
        if(empty($source_pic)){
            return redirect()->back()->with('error_message','找不到对应素材');
        }
        $source_pic->small_image=ImgTool::getImgUrlByImgPath($source_pic->small_image);
        return view('Front.sourcepic.updateSourcePic')->with('source_pic',$source_pic);
    }

    /****
     * 更新素材
     * @param Request $request
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
     */
    public function updateSourcePic(Request $request){
        try{
        $rule=array(
            'source_pic_id'=>'required|string|',
            'name'=>'required|string|',
            'limited_time_type'=>'required|string|',
            'price'=>'required|numeric|min:0',
        );
        $validator=SourcePicTool::store($request,$rule);
        $source_pic_id=trim($request->input('source_pic_id'));
        $limited_time=trim($request->input('limited_time_type'));
        $end_time_str=trim($request->input('end_time'));
        $name=trim($request->input('name'));
        $price=floatval($request->input('price'));
        $val_data['source_pic_id']=$source_pic_id;
        $val_data['limited_time']=$limited_time;
        $val_data['end_time_str']=$end_time_str;
        $val_data['name']=$name;
        $source_pic=SourcePicFrontService::getSourcePic($val_data['source_pic_id']);
        if(empty($source_pic)){
            $result['state']=-2;
            $result['message']='素材不存在';
        }else if($source_pic->audit_state===1){
            $result['state']=-3;
            $result['message']='审核中,不能修改';
        }
        if($source_pic->audit_state===0){
            $val_data['audit_state']=1;
        }
        if(isset($result)&&(sizeof($result)>1)){
            return response()->json($result);
        }
        if ($validator->fails())
        {
            if($validator->errors()->first('name')){
                $result['state']=-4;
                $result['message']='素材名称格式错误';
            }else if($validator->errors()->first('price')){
                $result['state']=-5;
                $result['message']='素材单价格式错误';
            }
            if(isset($result)&&(sizeof($result)>1)){
                return response()->json($result);
            }
        }
        if($val_data['limited_time']=='unlimited_time'){
            $val_data['is_infinite_time']=1;
            $val_data['end_time']=0;
        }else{
            $val_data['is_infinite_time']=1;
            $val_data['end_time']=strtotime($end_time_str);
        }
        $res=$this->sourcePicFrontSer->updateSourcePic($val_data);
        if($res){
            //推送消息
            $this->sourcePicFrontSer->sourcePicPush($source_pic,$source_pic->user_id);
            if($source_pic->price!==$price){
                $price=SourcePicTool::getSourcePicPrice($source_pic_id,1);
                $input['modify_price']=$source_pic->price;
                $input['price']=$price;
                $input['resource_type']=1;
                $input['resource_id']=$source_pic_id;
                $input['admin_id']=0;
                $input['admin_email']='';
                @$update_modify_price_result=$this->modifyResourcePriceRecordsSer->save_modify_resource_price_record($input);

            }
            $result['state']=1;
            $result['message']='更新成功';
        }else{
            $result['state']=-1;
            $result['message']='更新失败';
        }
            return response()->json($result);
        }catch (\Exception $e){
            Log::info($e);
            $result['state']=-1;
            $result['message']='更新失败';
            return response()->json($result);
        }
    }

    /***
     * 素材删除
     * @param Request $request
     * @return \Illuminate\Http\RedirectResponse
     */
    public function deleteSourcePic(Request $request){
        try{
            $rule=array(
                'id'=>'required|string|',
            );
            $source_pic_id=trim($request->input('id'));
            $val_data['source_pic_id']=$source_pic_id;
            $source_pic=SourcePicFrontService::getSourcePic($source_pic_id,1);
            if(empty($source_pic)){
                $result['state']=-1;
                $result['message']='找不到对应素材！';
                return response()->json($result);
            }
            $user_id=$request->session()->get('user_id');
            if($source_pic->user_id!==$user_id){
                $result['state']=-4;
                $result['message']='无权修改';
                return response()->json($result);
            }
            $res=$this->sourcePicFrontSer->delSourcePic($source_pic_id);
            if($res){
                $result['state']=1;
                $result['message']='删除成功';
            }else{
                $result['state']=-1;
                $result['message']='删除失败';
            }
            return response()->json($result);
        }catch (\Exception $e){
            Log::info($e);
            return redirect()->back()->with('error_message','提交失败，请稍后再试');
        }
    }
}